<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        li {
            list-style-type: none;
        }

        .checkerBoard {
            width: calc(40vw + 20px);
            height: calc(40vw +20px);
            display: flex;
            flex-wrap: wrap;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            box-shadow: 14px 14px 10px #8b8b8b;

        }

        .board {
            width: 4vw;
            height: 4vw;
            text-align: center;
            line-height: 4vw;
            border-radius: 10%;
            background-color: #dee2e1c7;
            border: 1px solid #757171;
        }

        .pieces-box {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: calc(44vw + 22px);
            height: calc(44vw + 22px);
            display: flex;
            flex-wrap: wrap;
        }

        .pieces {
            width: 4vw;
            height: 4vw;
            border: 1px solid rgba(0, 0, 0, .1);
            display: flex;
            align-items: center;
            justify-content: center;

        }

        .piece {
            height: 2vw;
            width: 2vw;
            text-align: center;
            line-height: 2vw;
            color: #fff;
            border-radius: 50%;
            box-shadow: 5px 5px 10px #242324;
        }
    </style>
</head>

<body>
    <div id="app">
        <ul class="checkerBoard">
            <li v-for="(item,index) in 10*10" :key="'line'+index" class="board"></li>
        </ul>
        <ul class="pieces-box" @click="addPiece">
            <li class="pieces" v-for="(item,index) in statusList" :key="'piece'+index">
                <div class="piece" v-if="item" v-redorblack></div>
            </li>
        </ul>
    </div>

    <script src="../lib/vue.js"></script>
    <script>
        new Vue({
            el: "#app",
            data: {
                piecesList: [],
                statusList: []
            },
            directives: {
                redorblack: {
                    inserted(el) {
                        el.style.backgroundColor = Math.random() > 0.5 ? 'black' : 'white';
                    }
                }
            },
            created() {
                for (let i = 0; i < 121; i++) {
                    this.statusList.push(false);
                    this.piecesList.push(i)
                }
            },
            methods: {
                addPiece() {
                    if (this.statusList.includes(false)) {
                        let index = ~~(Math.random() * this.piecesList.length);
                        this.statusList[this.piecesList[index]] = true;
                        this.statusList = [...this.statusList];
                        this.piecesList.splice(index, 1);
                    } else {
                        alert('位置已填满!');
                    }
                }
            }
        })
    </script>
</body>

</html>